package lex.dm.ui.dbtree;

import java.sql.Connection;
import java.sql.SQLException;
import lex.dm.utils.DBUtils;
import lex.dm.utils.FieldInfo;

public class DBTreeUtils {
	private static final String ROOT_NAME = "data";

	public static DBNode createRootNode( Connection con ) throws SQLException
	{
		DBNode root = DBNode.newRootNode( ROOT_NAME );
		for( String table : DBUtils.getTableNames( con, null ) )
		{
			DBNode tableNode = DBNode.newTableNode( table );
			for( FieldInfo field : DBUtils.getFields( con, null, table ) )
			{
				DBNode fieldNode = DBNode.newFieldNode( field.getName() );
				fieldNode.add( DBNode.newTypeNode( field.getTypeName() ) );
				tableNode.add(fieldNode );
			}
			root.add( tableNode );
		}
		return root;
	}
}
